package com.kecheng.action;

import com.kecheng.com.utils.BeanHelper;
import com.kecheng.com.utils.CommonUtil;
import com.kecheng.dao.BannerDao;
import com.kecheng.dao.PeizhiDao;
import com.kecheng.dao.TeamDao;
import com.kecheng.dto.WebCommonsResponse;
import com.kecheng.pojo.Banner;
import com.kecheng.pojo.Peizhi;
import com.kecheng.pojo.Team;
import com.kecheng.util.FileUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

@ParentPackage("json-default")
@Action(value = "banner", results = {
        @Result(name = "success", location = "/admin/product/banner.jsp"),
        @Result(name = "fail", location = "/fail.jsp"),
        @Result(name = "webResponse", type = "json", params = {"root", "webCommonsResponse"})
})
@AllowedMethods(value = {("regex:.*")})
public class BannerAction extends BaseAction {
    @Autowired
    private BannerDao bannerDao;
    private Banner banner;
    private List<Banner> banners;
    private int[] id;
    private File file;
    private String fileFileName;
    private String fileContentType;
    private String msg;
    private Peizhi peizhi;
    @Autowired
    private PeizhiDao peizhiDao;
    private Team team;
    @Autowired
    private TeamDao teamDao;
    private String code;

    /**
     * 后台查出所有jsp
     *
     * @return
     */
    public String findall() {
        try {
            banners = bannerDao.findall();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * 后台更新轮播图设置提交
     *
     * @return
     * @throws IOException
     */
    @Transactional(readOnly = false)
    public String update() throws IOException {
        Banner bannerDb = bannerDao.findbyid(banner.getId());

        fileSave(bannerDb);

        if (CommonUtil.isEmpty(banner.getLink())) {
            banner.setLink("#");
        }            FileUtil.mkDir(root);

        //更新别的非空属性
        BeanHelper.copyNonNullProperties(banner, bannerDb);
        return findall();
    }

    /**
     * file里面有值,会保存,然后写入到bannerDb::setImg里面
     *
     * @param bannerDb
     * @throws IOException
     */
    private void fileSave(Banner bannerDb) throws IOException {
        if (CommonUtil.isNotEmpty(file)) {
            BufferedImage read = ImageIO.read(file);

            //生成文件名
            String root = ServletActionContext.getServletContext().getRealPath("/upload");
            int index = fileFileName.lastIndexOf('.');
            String formatName = fileFileName.substring(++index, fileFileName.length());
            String fn = System.currentTimeMillis() + "." + formatName;
            FileUtil.mkDir(root);

            //写图片到硬盘,地址入库
            ImageIO.write(read, formatName, new File(root, fn));
            bannerDb.setImg("upload/" + fn);
        }
    }

    public String findbyid() {
        banner = bannerDao.findbyid(banner.getId());
        return null;
    }

    /**
     * 新增一个轮播图
     *
     * @return
     * @throws IOException
     */
    @Transactional(readOnly = false)
    public String add() throws IOException {
        fileSave(banner);

        if (CommonUtil.isEmpty(banner.getLink())) {
            banner.setLink("#");
        }

        bannerDao.add(banner);
        return findall();
    }

    /**
     * 删除图片
     *
     * @return
     */
    @Transactional(readOnly = false)
    public String delete() {
        banner = bannerDao.findbyid(banner.getId());
        if (CommonUtil.isNotEmpty(banner)) {
            bannerDao.delete(banner);
        }
        return findall();
    }

    public String deletemore() {
        for (int i = 0; i < id.length; i++) {
            try {
                banner = bannerDao.findbyid(id[i]);
                bannerDao.delete(banner);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        bannerDao.delete(banner);
        return findall();
    }


    /**
     * 微信主页查出轮播图
     *
     * @return
     */
    public String wsSelectAll() {
        banners = bannerDao.findall();
        webCommonsResponse = WebCommonsResponse.ok(banners);
        return "webResponse";
    }


    public Banner getbanner() {
        return banner;
    }

    public void setBanner(Banner banner) {
        this.banner = banner;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Banner getBanner() {
        return banner;
    }

    public List<Banner> getBanners() {
        return banners;
    }

    public void setBanners(List<Banner> banners) {
        this.banners = banners;
    }

    public int[] getId() {
        return id;
    }

    public void setId(int[] id) {
        this.id = id;
    }

    public Peizhi getPeizhi() {
        return peizhi;
    }

    public void setPeizhi(Peizhi peizhi) {
        this.peizhi = peizhi;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
